const db = require('./connector');

const selectAllSchema = () => {
    const sql = `SELECT SCHEMA_NAME AS Database FROM INFORMATION_SCHEMA.SCHEMATA;`
    return new Promise((resolve, reject) => {
        db.query(sql, (err, res) => {
            if(err) return reject(err)
            resolve(res)
        })
    })
}

const selectAllTbName = (schema) => {
    const sql = `select table_name from information_schema.TABLES where TABLE_SCHEMA='${schema}' and TABLE_TYPE = 'base table';`
    return new Promise((resolve, reject) => {
        db.query(sql, (err, res) => {
            if(err) return reject(err)
            resolve(res)
        })
    })
}

const describeTable = (tbname) => {
    const sql = `desc ${tbname};`
    return new Promise((resolve, reject) => {
        db.query(sql, (err, res) => {
            if(err) return reject(err)
            resolve(res)
        })
    })
}

module.exports = {
    selectAllSchema,
    selectAllTbName,
    describeTable
}